package vn.com.shoppie.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ZoomButtonsController.OnZoomListener;

import com.google.android.maps.MapView;

public class ShopMapView extends MapView implements OnZoomListener{
	private int zoom;
    private ZoomListener zoomListener;
	public ShopMapView(Context arg0, AttributeSet arg1) {
		super(arg0, arg1);
	}

	@Override
	public void onVisibilityChanged(boolean arg0) {
		
	}
	public void setZoomLevel(int zoom) {
        this.zoom = zoom;
        this.getController().setZoom(zoom);
    }
    public ZoomListener getZoomListener() {
        return zoomListener;
    }
    public void setZoomListener(ZoomListener listener) {
        this.zoomListener = listener;
        this.getZoomButtonsController().setOnZoomListener(this);
    }
    public boolean onTouchEvent(MotionEvent event) {
        boolean result = super.onTouchEvent(event);
        if (event.getAction() == MotionEvent.ACTION_UP) {
            checkForZoomEvent();
        }
        return result;
    }
    private void checkForZoomEvent() {
        if (this.zoomListener != null) {
            int newZoom = this.getZoomLevel();
            if (newZoom != zoom) {
                this.zoomListener.onZoom(zoom, newZoom);
            }
            zoom = newZoom;
        }
    }
	@Override
	public void onZoom(boolean zoomIn) {
        int oldZoom = this.zoom;
        this.setZoomLevel(oldZoom + (zoomIn ? 1 : -1));
        if (this.zoomListener != null) {
            this.zoomListener.onZoom(oldZoom, this.zoom);
        }
    }
	public interface ZoomListener {
	    public void onZoom(int oldZoom, int newZoom);
	}
	
}
